package org.jet.emall.rbac.service;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jet.emall.common.restful.result.CommonPage;
import org.jet.emall.rbac.dto.AdminLoginLogDTO;
import org.jet.emall.rbac.dto.AdminOpLogDTO;
import org.jet.emall.rbac.po.AdminLoginLog;
import org.jet.emall.rbac.po.AdminOpLog;
import org.jet.emall.rbac.service.mp.IAdminLoginLogService;
import org.jet.emall.rbac.service.mp.IAdminOpLogService;
import org.jet.emall.rbac.transfer.AdminLogDtoTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-01 16:14
 */
@Service
public class CustomAdminLogServiceImpl implements CustomAdminLogService {

    @Autowired
    private IAdminOpLogService adminOpLogService;

    @Autowired
    private IAdminLoginLogService adminLoginLogService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOperationLog(AdminOpLogDTO adminOpLogDTO) {
        AdminOpLog adminOpLog = AdminLogDtoTransfer.INSTANCE.toPo(adminOpLogDTO);
        adminOpLogService.save(adminOpLog);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveLoginLog(AdminLoginLogDTO dto) {
        AdminLoginLog adminLoginLog = AdminLogDtoTransfer.INSTANCE.toPo(dto);
        adminLoginLogService.save(adminLoginLog);
    }

    @Override
    public CommonPage<AdminLoginLogDTO> getLoginLogPage(String query, Integer pageNum, Integer pageSize,Integer userId,Integer roleId) {
        IPage<AdminLoginLog> page = new Page<>();
        page.setCurrent(pageNum).setSize(pageSize);
        Wrapper<AdminLoginLog> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(query)){
            ((QueryWrapper<AdminLoginLog>) wrapper).like("username",query)
                    .or().like("role_name",query);
        }
        // 如果不是1号角色则只能查看用户自己的日志
        if(roleId!=1){
            ((QueryWrapper<AdminLoginLog>) wrapper).eq("user_id",userId);
        }
        adminLoginLogService.page(page,wrapper);
        CommonPage<AdminLoginLogDTO> commonPage = new CommonPage<>();
        commonPage.setTotal(page.getTotal())
                .setPageNum(page.getCurrent())
                .setPageSize(page.getSize())
                .setTotalPage(page.getPages())
                .setList(AdminLogDtoTransfer.INSTANCE.toPoList(page.getRecords()));

        return commonPage;
    }

    @Override
    public CommonPage<AdminOpLogDTO> getOperationLogPage(String query, Integer pageNum, Integer pageSize,Integer userId,Integer roleId) {
        IPage<AdminOpLog> page = new Page<>();
        page.setCurrent(pageNum).setSize(pageSize);
        Wrapper<AdminOpLog> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(query)){
            ((QueryWrapper<AdminOpLog>) wrapper).like("username",query)
                    .or().like("role_name",query)
                    .or().like("func",query);
        }
        // 如果不是1号角色则只能查看用户自己的日志
        if(roleId!=1){
            ((QueryWrapper<AdminOpLog>) wrapper).eq("user_id",userId);
        }
        adminOpLogService.page(page,wrapper);
        CommonPage<AdminOpLogDTO> commonPage = new CommonPage<>();
        commonPage.setTotal(page.getTotal())
                .setPageNum(page.getCurrent())
                .setPageSize(page.getSize())
                .setTotalPage(page.getPages())
                .setList(AdminLogDtoTransfer.INSTANCE.toOpPoList(page.getRecords()));

        return commonPage;
    }
}
